package com.example.music.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dto.CommentDto;
import com.example.music.entity.CmtUp;
import com.example.music.entity.Comment;
import com.example.music.service.CmtUpService;
import com.example.music.service.CommentService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ResponseMessage;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.ConnectException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    CommentService commentService;
    @Autowired
    CmtUpService cmtUpService;

    @PostMapping("up")
    public ResponseMessage upByUserIdType(@RequestBody CommentDto commentDto){
        return null;
    }

    @PostMapping("add")
    public AjaxJson add(@RequestBody Comment comment){
        Integer add = commentService.add(comment);
        if (add < 1){
            return AjaxJson.getError();
        }else {
            return AjaxJson.getSuccess();
        }
    }

    @PostMapping("update")
    public AjaxJson update(@RequestBody Comment comment){
        UpdateWrapper<Comment> commentUpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper<Comment> id = commentUpdateWrapper.eq("id", comment.getId());
        int update = commentService.update(comment, id);
        return AjaxJson.getSuccess(String.valueOf(update));
    }

    @SaCheckLogin
    @PostMapping("delete")
    public AjaxJson delete(@RequestParam String id){
        Integer integer = commentService.deleteById(id);
        return AjaxJson.getSuccess(integer.toString());
    }

    @GetMapping("commitBySongId")
    public AjaxJson commitBySongId(@RequestParam String SongId){
        List<Comment> song_id = commentService.selectItemsByParam("song_id", SongId);
        song_id.forEach(e->{
            e.setUp(cmtUpService.getAllById(e.getId().intValue()));
        });
        return AjaxJson.getSuccessData(song_id);
    }

    @GetMapping("commitBySongListId")
    public AjaxJson commitBySongListId(@RequestParam String SongListId){
        List<Comment> song_id = commentService.selectItemsByParam("song_list_id", SongListId);
        song_id.forEach(e->{
            e.setUp(cmtUpService.getAllById(e.getId().intValue()));
        });
        return AjaxJson.getSuccessData(song_id);
    }

    @PostMapping("cmt/like")
    public AjaxJson like(@RequestBody CmtUp cmtUp){
        List<CmtUp> cmtUps = cmtUpService.selectAll();
        boolean b = cmtUps.parallelStream().anyMatch(e -> (
                e.getCommitId().equals(cmtUp.getCommitId()) && e.getUserId().equals(cmtUp.getUserId())
        ));
        if (b){
            return AjaxJson.getError();
        }else {
            CmtUp cmtUp1 = new CmtUp(cmtUp.getCommitId(), cmtUp.getUserId());
            cmtUpService.add(cmtUp1);
            return AjaxJson.getSuccess();
        }
    }

    @PostMapping("cmt/allupBycommitId")
    public AjaxJson like(@RequestParam String commitId){
        List<CmtUp> commit_id = cmtUpService.selectItemsByParam("commit_id", commitId);
        return AjaxJson.getSuccess(String.valueOf(commit_id.size()));
    }

}
